package com.biyabi.controller;

import com.biyabi.entity.Product;
import com.biyabi.pojo.ProductsData;
import com.biyabi.pojo.Result;
import com.biyabi.service.ProductService;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/back/product")
public class ProductController {
    @Resource
    private ProductService productService;
    @RequestMapping("/add")
    public Result add(Product product, BindingResult bindingResult, MultipartFile addProductCover, MultipartFile[] addProductTop, MultipartFile[] addProductDown,String productStatus, String catelogId, String brandId, String skuPojo,  HttpServletRequest request){
        Result result = new Result();
        try{
            product.setStatus("on".equals(productStatus)?1:0);
            productService.add(product,addProductCover,addProductTop,addProductDown,catelogId,brandId,skuPojo,request);
            result.setStatus(true);
            result.setMessage("添加商品成功！");
        }catch(Exception e){
            result.setStatus(false);
            result.setMessage("添加商品失败！");
        }
        return result;
    }
    @RequestMapping("/update")
    public Result update(Product product, BindingResult bindingResult,  MultipartFile[] updateProductTop, MultipartFile[] updateProductDown,String productStatus, String catelogId, String brandId, String skuPojo,  HttpServletRequest request){
        Result result = new Result();
        try{
            product.setStatus("on".equals(productStatus)?1:0);
            productService.update(product,updateProductTop,updateProductDown,productStatus,catelogId,brandId,skuPojo,request);
            result.setStatus(true);
            result.setMessage("更新商品成功！");
        }catch(Exception e){
            result.setStatus(false);
            result.setMessage("更新商品失败！");
        }
        return result;
    }
    @RequestMapping("/findAll")
    public ProductsData findAll(Integer page, Integer rows, String columnName, String columnValue){
        return productService.findAll(page,rows,columnName,columnValue);
    }
}
